package com.example.myapplication.test2;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class GroupListAdapter extends RecyclerView.Adapter<GroupListAdapter.Holder> {
    private static final String TAG = "CountDownTimerAdapter->";
    //数据
    private List<CourseBean> mList;
    //线程调度，用来更新列表
    private Handler mHandler;

    private Timer mTimer;
    private MyTask mTask;

    public GroupListAdapter() {
        mList = new ArrayList<>();
        mHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        notifyItemChanged(msg.arg1, "update-time");
                        break;
                }
            }
        };
        mTask = new MyTask();
    }

    public void bindAdapterToRecyclerView(@NonNull RecyclerView view) {
        view.setAdapter(this);
    }

    /**
     * 设置新的数据源
     *
     * @param list 数据
     */
    public void setNewData(@NonNull List<CourseBean> list) {
        destroy();
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
        if (mTimer == null) {
            mTimer = new Timer();
        }
        mTimer.schedule(mTask, 0, 1000);
    }

    /**
     * 销毁资源
     */
    public void destroy() {
        mHandler.removeMessages(1);
        if (mTimer != null) {
            mTimer.cancel();
            mTimer.purge();
            mTimer = null;
        }
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_count_down_timer, viewGroup, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position, @NonNull List<Object> payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
            return;
        }
        //更新某个控件,比如说只需要更新时间信息，其他不用动
        CourseBean bean = mList.get(position);
        long day = bean.getTotalTime() / (1000 * 60 * 60 * 24);
        long hour = (bean.getTotalTime() / (1000 * 60 * 60) - day * 24);
        long min = ((bean.getTotalTime() / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (bean.getTotalTime() / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        holder.tvTime.setText("倒计时: " + day + "天" + hour + "小时" + min + "分" + s + "秒");
//        holder.btnAction.setText(bean.isPause() ? "开始" : "暂停");
        holder.btnAction.setEnabled(bean.getTotalTime() != 0);
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, int position) {
        holder.ivIcon.setImageResource(R.mipmap.ic_launcher_round);
        final CourseBean bean = mList.get(position);
        long day = bean.getTotalTime() / (1000 * 60 * 60 * 24);
        long hour = (bean.getTotalTime() / (1000 * 60 * 60) - day * 24);
        long min = ((bean.getTotalTime() / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (bean.getTotalTime() / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        holder.tvTime.setText("倒计时: " + day + "天" + hour + "小时" + min + "分" + s + "秒");
//        holder.btnAction.setText(bean.isPause() ? "开始" : "暂停");
        holder.btnAction.setEnabled(bean.getTotalTime() != 0);
        holder.btnAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean.isPause()) {
                    bean.setPause(false);
                    holder.btnAction.setText("暂停");
                } else {
                    bean.setPause(true);
                    holder.btnAction.setText("开始");
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        private ImageView ivIcon;
        private TextView tvTime;
        private Button btnAction;

        Holder(@NonNull View itemView) {
            super(itemView);
            ivIcon = itemView.findViewById(R.id.iv_icon);
            tvTime = itemView.findViewById(R.id.tv_time);
            btnAction = itemView.findViewById(R.id.btn_action);
        }
    }

    class MyTask extends TimerTask {
        @Override
        public void run() {
            if (mList.isEmpty()) {
                return;
            }
            int size = mList.size();
            CourseBean bean;
            long totalTime;
            for (int i = 0; i < size; i++) {
                bean = mList.get(i);
                if (!bean.isPause()) {//不处于暂停状态
                    totalTime = bean.getTotalTime() - 1000;
                    if (totalTime <= 0) {
                        bean.setPause(true);
                        bean.setTotalTime(0);
                    }
                    bean.setTotalTime(totalTime);
                    Message message = mHandler.obtainMessage(1);
                    message.arg1 = i;
                    mHandler.sendMessage(message);
                }
            }
        }
    }
}
